package core

import (
	"huwhy.cn/go-spider/model"
	"huwhy.cn/go-spider/util"
)

type RequestWorker struct {
	id     int
	state  bool
	spider *Spider
}

func (w *RequestWorker) Run() {
	for w.state {
		request := w.spider.taskQueue.Pop().(*model.Request)
		w.doRequest(request)
	}
}

func (w *RequestWorker) doRequest(request *model.Request) {
	body, err := util.Request(request.URI, request.Method)
	w.spider.AddResult(&model.Response{
		Data:    body,
		Err:     err,
		Request: request,
	})
}

func (w *RequestWorker) Stop() {
	w.state = false
}
